package com.xinyin.aiqinhaiback.service.handStrategy;

import com.xinyin.aiqinhaiback.Utils.InformationTreeUtil;
import com.xinyin.aiqinhaiback.common.Enum.ResultEnum;
import com.xinyin.aiqinhaiback.common.ResponseResult;
import com.xinyin.aiqinhaiback.exception.MyException;
import com.xinyin.aiqinhaiback.mapper.InformationMapper;
import com.xinyin.aiqinhaiback.modbus.Modbus4jWriteUtils;
import com.xinyin.aiqinhaiback.modbus.ModbusUtil;
import com.xinyin.aiqinhaiback.model.entity.Information;
import com.xinyin.aiqinhaiback.model.entity.ModBusDevice;
import com.xinyin.aiqinhaiback.model.entity.Param;
import com.xinyin.aiqinhaiback.service.HandleStrategy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service
public class HeatHandleStrategy  implements HandleStrategy  {


	@Resource
	private InformationMapper informationMapper;
	public Modbus4jWriteUtils modbus4jWriteUtilsHeat;


	private HeatHandleStrategy(){
		modbus4jWriteUtilsHeat = new Modbus4jWriteUtils("192.168.1.20");
	}

	@Override
	public ResponseResult updateConfig(Param param) {
		try {
			modbus4jWriteUtilsHeat.write(param.getModbus(), param.getValue());
		} catch (Exception e) {
			throw new MyException(ResultEnum.WIRTEFAIL);
		}
		return new ResponseResult();
	}

	@Override
	public ResponseResult<List<Map<String, Object>>> selectConfig(Param param) throws Exception {
		List<Information> informationList = informationMapper.getAllHeatSourceDevice(); ;
		ModBusDevice modBusDevice = ModbusUtil.getInformationData(informationList);
		List<Map<String, Object>> resList = InformationTreeUtil.getData(modBusDevice.getInformationList());
		return new ResponseResult<>(resList);
	}
}
